#include <QFileDialog>
#include <QMessageBox>
#include "gamelobbydialog.h"
#include "ui_gamelobbydialog.h"
#include "mainwindow.h"
#include "../GameInterface/networkinterface.h"
#include "../GameInterface/gameinterface.h"
#include "../GameInterface/cpuinterface.h"

GameLobbyDialog::GameLobbyDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::GameLobbyDialog)
{
    ui->setupUi(this);
}

GameLobbyDialog::~GameLobbyDialog()
{
    delete ui;
}

void GameLobbyDialog::InitializeLobby()
{
    QString PlayerName = ((MainWindow*)parent()->parent())->GetPlayerName();
    if(PlayerName.isEmpty()) PlayerName = "Unknown";

    Settings.Initialize();

    PlayerInfo player = {new GameInterface(), PlayerName, ""};
    Settings.Teams[0].append(player);

    ui->buttonTeam1->setDisabled(true);
    ui->buttonTeam2->setEnabled(true);

    UpdatePlayers();

    //connect(&NetworkInterface::GameServer, SIGNAL(newConnection()), this, SLOT(PlayerConnected()));
    //NetworkInterface::StartServer();
}

void GameLobbyDialog::UpdatePlayers()
{
    QVector<QString> Names;
    Names.resize(12);
    for(int team = 0; team < 2; ++team)
    for(int slot = 0; slot < Settings.Teams[team].count(); ++slot)
        Names[team*6 + slot] = Settings.Teams[team][slot].Name;

    ui->PlayerName1->setText(Names[0]);
    ui->PlayerName2->setText(Names[1]);
    ui->PlayerName3->setText(Names[2]);
    ui->PlayerName4->setText(Names[3]);
    ui->PlayerName5->setText(Names[4]);
    ui->PlayerName6->setText(Names[5]);
    ui->PlayerName7->setText(Names[6]);
    ui->PlayerName8->setText(Names[7]);
    ui->PlayerName9->setText(Names[8]);
    ui->PlayerName10->setText(Names[9]);
    ui->PlayerName11->setText(Names[10]);
    ui->PlayerName12->setText(Names[11]);
}

void GameLobbyDialog::PlayerConnected()
{
    //QTcpSocket *NewConnection = NetworkInterface::GameServer.nextPendingConnection();
    if(Settings.Teams[0].count() == 6 && Settings.Teams[1].count() == 6)
    {
        return;
    }

    int team;
    if(Settings.Teams[0].count() > Settings.Teams[1].count())
    {
        team = 1;
    }
    else
    {
        team = 0;
    }

    QString fileName = QFileDialog::getOpenFileName(this, "Open File",
                                                     "",
                                                     "Library files (*.dll)");
    if(!fileName.isEmpty())
    {
        try
        {
            PlayerInfo player = {new CpuInterface(fileName), "Bob", ""};
            Settings.Teams[team].append(player);
            UpdatePlayers();
        }
        catch(const char *ex)
        {
            QMessageBox::critical(this, "Error", ex);
        }
    }
}

void GameLobbyDialog::PlayerDisconnected()
{

}

void GameLobbyDialog::on_pushButton_2_clicked()
{
    close();
}

void GameLobbyDialog::on_pushButton_3_clicked()
{
    PlayerConnected();
}

void GameLobbyDialog::on_pushButton_clicked()
{
    ((MainWindow*)(parent()->parent()))->PrepareGame(Settings);
    close();
}

void GameLobbyDialog::SwitchTeam(int newteam)
{
    if(Settings.MyTeam != newteam)
    {
        int oldteam = newteam ? 0 : 1;
        if(Settings.Teams[newteam].count() < 6)
        {
            Settings.Teams[newteam].append(Settings.Teams[oldteam][Settings.MyPosition]);
            Settings.Teams[oldteam].remove(Settings.MyPosition);

            Settings.MyTeam = newteam;
            Settings.MyPosition = Settings.Teams[newteam].count() - 1;

            if(newteam == 0)
            {
                ui->buttonTeam1->setDisabled(true);
                ui->buttonTeam2->setEnabled(true);
            }
            else
            {
                ui->buttonTeam1->setEnabled(true);
                ui->buttonTeam2->setDisabled(true);
            }
            UpdatePlayers();
        }
    }
}

void GameLobbyDialog::on_buttonTeam1_clicked()
{
    SwitchTeam(0);
}

void GameLobbyDialog::on_buttonTeam2_clicked()
{
    SwitchTeam(1);
}
